package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.domain.PmsSkuImages;
import com.sky.domain.PmsSkuInfo;
import com.sky.domain.PmsSkuSaleAttrValue;
import com.sky.mapper.PmsSkuImagesMapper;
import com.sky.mapper.PmsSkuSaleAttrValueMapper;
import com.sky.service.PmsSkuInfoService;
import com.sky.mapper.PmsSkuInfoMapper;
import com.sky.utils.PageUtils;
import com.sky.utils.StringUtils;
import com.sky.vo.param.PmsSkuInfoVO;
import com.sky.vo.sku.SkuInfoVo;
import com.sky.vo.sku.SpuSaleAttrVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【pms_sku_info(sku信息)】的数据库操作Service实现
* @createDate 2024-06-30 16:46:42
*/
@Service
public class PmsSkuInfoServiceImpl extends ServiceImpl<PmsSkuInfoMapper, PmsSkuInfo> implements PmsSkuInfoService{

    @Resource
    private PmsSkuImagesMapper pmsSkuImagesMapper;
    @Resource
    private PmsSkuSaleAttrValueMapper pmsSkuSaleAttrValueMapper;

    /**
     * 分页查询
     * @param vo
     * @return
     */
    @Override
    public PageUtils pageQuery(PmsSkuInfoVO vo) {
        LambdaQueryWrapper<PmsSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(vo.getKey())) {
            queryWrapper.or(wq -> {
                wq.like(PmsSkuInfo::getSkuName, vo.getKey())
                        .or()
                        .like(PmsSkuInfo::getSkuId, vo.getKey())
                        .or()
                        .like(PmsSkuInfo::getSkuDesc, vo.getKey());
            });
        }
        if (StringUtils.isNotEmpty(vo.getMin()) && vo.getMin().compareTo(BigDecimal.ZERO) > 0){
            queryWrapper.ge(PmsSkuInfo::getPrice, vo.getMin());
        }
        if (StringUtils.isNotEmpty(vo.getMax()) && vo.getMax().compareTo(BigDecimal.ZERO) > 0){
            queryWrapper.le(PmsSkuInfo::getPrice, vo.getMax());
        }
        if (StringUtils.isNotEmpty(vo.getCatalogId())){
            queryWrapper.eq(PmsSkuInfo::getCatalogId, vo.getCatalogId());
        }
        if (StringUtils.isNotEmpty(vo.getBrandId())){
            queryWrapper.eq(PmsSkuInfo::getBrandId, vo.getBrandId());
        }
        return PageUtils.selectPage(this.baseMapper, queryWrapper);
    }

    /**
     * 商品详情页数据查询
     *
     * @param skuId
     */
    @Override
    public SkuInfoVo getSkuDetails(Long skuId) {
        SkuInfoVo skuInfoVo = new SkuInfoVo();
        // 商品基本信息
        PmsSkuInfo pmsSkuInfo = baseMapper.selectById(skuId);
        skuInfoVo.setSkuId(skuId);
        skuInfoVo.setSkuName(pmsSkuInfo.getSkuName());
        skuInfoVo.setSkuDefaultImg(pmsSkuInfo.getSkuDefaultImg());
        skuInfoVo.setPrice(pmsSkuInfo.getPrice());
        skuInfoVo.setWeight("0");

        // 商品图片集
        List<PmsSkuImages> pmsSkuImages = pmsSkuImagesMapper.selectList(new LambdaQueryWrapper<PmsSkuImages>().eq(PmsSkuImages::getSkuId, skuId));
        skuInfoVo.setSkuImageList(pmsSkuImages);

        // 查询商品售卖属性
        List<PmsSkuSaleAttrValue> pmsSkuSaleAttrValues = pmsSkuSaleAttrValueMapper.selectList(new LambdaQueryWrapper<PmsSkuSaleAttrValue>().eq(PmsSkuSaleAttrValue::getSkuId, skuId));
        Map<String, List<PmsSkuSaleAttrValue>> map = pmsSkuSaleAttrValues.stream().collect(Collectors.groupingBy(PmsSkuSaleAttrValue::getAttrName));
        List<SpuSaleAttrVo> saleAttrVoList = map.entrySet().stream().map(entry -> {
            SpuSaleAttrVo spuSaleAttrVo = new SpuSaleAttrVo();
            spuSaleAttrVo.setAttrName(entry.getKey());
            List<PmsSkuSaleAttrValue> attrValueList = entry.getValue();
            spuSaleAttrVo.setAttrValueList(attrValueList);
            return spuSaleAttrVo;
        }).collect(Collectors.toList());
        skuInfoVo.setSkuSaleAttrValueList(saleAttrVoList);

        return skuInfoVo;
    }

    /**
     * 根据skuId查询sku价格
     *
     * @param skuIds
     */
    @Override
    public Map<Long, BigDecimal> getSkuPriceByIds(Set<Long> skuIds) {
        Map<Long, BigDecimal> map = new HashMap<>();
        for (Long skuId : skuIds) {
            BigDecimal price = baseMapper.selectById(skuId).getPrice();
            map.put(skuId, price);
        }
        return map;
    }
}




